package com.ymx.darling.intercept;

import com.ymx.darling.executor.Executor;
import com.ymx.darling.session.setting.Context;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 拦截器的信息封装
 * @author 爱Java的小于
 */
public class Invocation {
    private final Executor executor; // 执行器
    private final Method method; //  方法
    private final Object[] args; // 参数
    private final String methodName; // 方法名
    private final Method mapperMethod; // mapper的方法实例

    public Invocation(Executor executor, Method method, Object[] args) {
        this.executor = executor;
        this.method = method;
        this.mapperMethod = Context.getMapperMethod();
        this.methodName = method.getName();
        this.args = args;
    }

    public String getMethodName() {
        return methodName;
    }

    public Method getMapperMethod() {
        return mapperMethod;
    }

    public Object[] getArgs() {
        return args;
    }

    public Method getMethod() {
        return method;
    }


    /**
     * 执行当前命令
     * 此方法使用Java反射机制来执行命令它通过调用与当前executor关联的方法来实现
     * 方法调用传递的参数是当前命令执行时需要的参数
     *
     * @return Object 执行结果对象
     * @throws InvocationTargetException 如果被调用的方法抛出异常
     * @throws IllegalAccessException 如果访问方法失败
     */
    public Object proceed() throws InvocationTargetException, IllegalAccessException {
        return this.method.invoke(this.executor, this.args);
    }

}
